/*
 * 公共url
 */

// var local = "http://localhost:8080/mars-admin/h5/";
var local = "";
var username = document.getElementById("username").value;;

var app = new Vue({
    el: '#app',
    data: {
        user: {},
        getSensorList: [],
        batteryList: [],
        alltoollist: [],
        toolList: [],
        type_a: "",
        type_b: "",
        type_c: "",
        is_a: 1,
        is_b: 0,
        is_c: 0,
        state: 0,
        stateText:"连接"
    },
    methods: {}
});

function tab(tab, type) {
    //切换
    $(".tab .line").addClass("com_none");
    $("." + tab).find(".line").removeClass("com_none");
    $("." + tab).addClass("main_color").siblings().removeClass("main_color");
    $("#" + tab).removeClass("com_none").siblings().addClass("com_none");
    switch(type) {
        case 1:
            //tab_a
            if(app.is_a == 0) {
                getSensorList();
            }
            break;
        case 2:
            //tab_b
            if(app.is_b == 0) {
                getBattery();
            }
            break;
        case 3:
            //tab_c
            if(app.is_c == 0) {
                getTooList();
            }
            break;
    }
}

//==============获取用户信息===========================
getUser()
getSensorList();

function getUser() {
    $.ajax({
        type: "get",
        url: local + "getuser",
        async: true,
        data: {
            username: username
        },
        success: function(data) {
            //			console.log(data);
            //			var res = JSON.parse(data);
            if(data.code == 0) {
                app.user = data.serviceengineer;
            } else {
                mui.toast(data.msg);
            }
        },
        error: function(err) {
            console.error(JSON.stringify(err));
        }
    });
}

//==================获取待维修传感器信息========================
function getSensorList() {
    $.ajax({
        type: "get",
        url: local + "getsensorlist",
        async: true,
        data: {
            username: username
        },
        success: function(data) {
            app.getSensorList = [];
            if(data.code == 0) {
                app.getSensorList = data.sensorlist;
                addMarker(app.getSensorList);
                app.is_a = 1;
            } else {
                mui.toast(data.msg);
            }
        },
        error: function(err) {
            console.error(JSON.stringify(err));
        }
    });
}

//==================提交完成========================
function subFin(th) {
    var sensorid = $(th).attr("sensorid");
    if(!sensorid) {
        mui.toast("获取提交数据失败");
        return;
    }
    var btnArray = ['取消', '确定'];
    mui.confirm('是否确认已完成？', '温馨提示', btnArray, function(e) {
        if(e.index == 0) {} else {
            $.ajax({
                type: "get",
                url: local + "updatebattstatus",
                async: true,
                data: {
                    username: username,
                    sensorid: sensorid
                },
                success: function(data) {
                    //					console.log(data);
                    if(data.code == 0) {
                        mui.alert("提交成功！");
                        getSensorList();
                    } else {
                        mui.toast(data.msg);
                    }
                },
                error: function(err) {
                    console.error(JSON.stringify(err));
                }
            });
        }
    })

}

//==================获取电池清单========================
function getBattery() {
    $.ajax({
        type: "get",
        url: local + "getbattery",
        async: true,
        data: {
            username: username
        },
        success: function(data) {
            //			console.log(data);
            app.batteryList = [];
            if(data.code == 0) {
                app.batteryList = data.batterylist;
                app.is_b = 1;
            } else {
                mui.toast(data.msg);
            }
        },
        error: function(err) {
            console.error(JSON.stringify(err));
        }
    });
}
//==================获取工具清单========================
function getTooList() {
    $.ajax({
        type: "get",
        url: local + "gettoolist",
        async: true,
        data: {
            username: username
        },
        success: function(data) {
            if(data.code == 0) {
                app.toolList = data.toollist;
                //				app.alltoollist = data.alltoollist;
                app.is_c = 1;
            } else {
                mui.toast(data.msg);
            }
        },
        error: function(err) {
            console.error(JSON.stringify(err));
        }
    });
}

/*
 * 弹出文字提示
 */
function alert_info(th) {
    var name = $(th).attr("name");
    if(name) {
        mui.alert(name);
    }
}

/*
 * 切换途径点 连接
 */
function selectState() {
    switch(app.state) {
        case 0:
            //取消连接，默认不连接
            getSensorList();
            app.state = 1;
            app.stateText="取消连接";
            break;
        case 1:
            //连接
            getSensorList();
            app.state = 0;
            app.stateText="连接";
            break;
    }
}